//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.games.CraftingEngine;

import com.comprosoft.telnet.io.IOHandler;
import com.comprosoft.telnet.io.TextColor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;

public class ElementCollection {
    static ArrayList<Element> AllElements;
    private ArrayList<Element> UnlockedElements = new ArrayList();
    private final IOHandler io;

    public ElementCollection(IOHandler io) throws IOException {
        this.io = io;
        if (AllElements == null) {
            AllElements = ReadFile();
        }

        this.FillUnlocked();
    }

    private static ArrayList<Element> ReadFile() throws IOException {
        ArrayList<Element> list = new ArrayList();
        InputStream is = CraftingEngine.class.getResourceAsStream("/CraftingRecepies.txt");
        BufferedReader textReader = new BufferedReader(new InputStreamReader(is));

        String line;
        while((line = textReader.readLine()) != null) {
            String[] RecipeParts = line.split(Pattern.quote(":"));
            String[] RecipeParts2 = RecipeParts[0].split(Pattern.quote(","));

            try {
                list.add(new Element(RecipeParts2[0], RecipeParts[1], TextColor.NameToColor(RecipeParts2[1])));
            } catch (Exception var7) {
                var7.printStackTrace();
            }
        }

        textReader.close();
        return list;
    }

    private void FillUnlocked() {
        for(int i = 0; i < 4; ++i) {
            this.UnlockedElements.add(AllElements.get(i));
        }

    }

    public void Reset() {
        this.UnlockedElements.clear();
        this.FillUnlocked();
        this.io.ClearScreen();
        CraftingEngine.DrawTitle(this.io);
    }

    public void Craft(String input) {
        String[] Inputs = input.split(Pattern.quote("+"));
        if (Inputs.length > 2) {
            this.io.SetTextColor(TextColor.RED);
            this.io.WriteLine("\nBad crafting recipe!");
            this.io.SetTextColor(TextColor.WHITE);
            this.io.WriteLine("");
        } else if (input.length() != 0) {
            if (Inputs.length == 1) {
                this.ShowRecipes(input.trim());
            } else {
                this.Craft(Inputs[0].trim(), Inputs[1].trim());
            }
        }
    }

    private void ShowRecipes(String input) {
        this.io.WriteLine("");
        boolean isFound = false;
        Iterator var3 = this.UnlockedElements.iterator();

        while(var3.hasNext()) {
            Element e = (Element)var3.next();
            if (e.GetName().toLowerCase().equals(input.toLowerCase())) {
                e.DrawRecipes(this.io);
                isFound = true;
            }
        }

        if (!isFound) {
            this.io.SetTextColor(TextColor.RED);
            this.io.WriteLine("Unknown element " + input);
            this.io.SetTextColor(TextColor.WHITE);
            this.io.WriteLine("");
        }

    }

    private boolean TestUnlocked(String input) {
        Iterator var2 = this.UnlockedElements.iterator();

        Element e;
        do {
            if (!var2.hasNext()) {
                return false;
            }

            e = (Element)var2.next();
        } while(!e.GetName().toLowerCase().equals(input.toLowerCase()));

        return true;
    }

    private boolean TestAllUnlocked() {
        return this.UnlockedElements.size() == AllElements.size();
    }

    private void Craft(String element1, String element2) {
        boolean toBreak = false;
        if (!this.TestUnlocked(element1)) {
            this.io.SetTextColor(TextColor.RED);
            this.io.WriteLine("\nUnknown element " + element1);
            toBreak = true;
        }

        if (!this.TestUnlocked(element2)) {
            this.io.SetTextColor(TextColor.RED);
            this.io.WriteLine("\nUnknown element " + element2);
            toBreak = true;
        }

        if (toBreak) {
            this.io.SetTextColor(TextColor.WHITE);
            this.io.WriteLine("");
        } else {
            boolean didCraft = false;
            Iterator var5 = AllElements.iterator();

            while(var5.hasNext()) {
                Element e = (Element)var5.next();
                if (e.TestElementCraftable(element1.toLowerCase(), element2.toLowerCase())) {
                    if (!didCraft) {
                        this.io.SetTextColor(TextColor.WHITE);
                        this.io.WriteLine("\nYou crafted:");
                        didCraft = true;
                    }

                    e.DrawElement(this.io);
                    if (!this.TestUnlocked(e.GetName())) {
                        this.UnlockedElements.add(e);
                    }
                }
            }

            this.io.WriteLine("");
            if (!didCraft) {
                this.io.SetTextColor(TextColor.RED);
                this.io.WriteLine("You didn't craft anything...");
                this.io.SetTextColor(TextColor.WHITE);
                this.io.WriteLine("");
            }
        }

    }

    public void List(String command) {
        ArrayList<Character> ToSearchFor = new ArrayList();
        boolean toBreak = false;
        String[] var4 = command.toLowerCase().split(Pattern.quote(" "));
        int var5 = var4.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String e = var4[var6];
            e.trim();
            if (e.length() == 1) {
                ToSearchFor.add(e.toCharArray()[0]);
            } else if (!e.equals("list") && e.length() != 0) {
                this.io.SetTextColor(TextColor.RED);
                this.io.WriteLine("\nInvalid character " + e + "\n");
                toBreak = true;
            }
        }

        if (!toBreak) {
            Set<Character> setWithUniqueValues = new HashSet(ToSearchFor);
            this.ListElements(new ArrayList(setWithUniqueValues));
        }

    }

    private void ListElements(ArrayList<Character> toFind) {
        Collections.sort(this.UnlockedElements);
        Collections.sort(toFind);
        Iterator var2;
        if (toFind.size() == 0) {
            this.io.SetTextColor(TextColor.WHITE);
            this.io.WriteLine("\nAvailable Elements:");
            var2 = this.UnlockedElements.iterator();

            while(var2.hasNext()) {
                Element e = (Element)var2.next();
                e.DrawElement(this.io);
            }

            this.io.WriteLine("");
        } else {
            this.io.WriteLine("");
            var2 = toFind.iterator();

            while(var2.hasNext()) {
                Character c = (Character)var2.next();
                this.io.SetTextColor(TextColor.WHITE);
                this.io.WriteLine("------" + c.toString().toUpperCase() + "------");
                boolean found = false;
                Iterator var5 = this.UnlockedElements.iterator();

                while(var5.hasNext()) {
                    Element e = (Element)var5.next();
                    if (e.GetName().substring(0, 1).toLowerCase().equals(c.toString().toLowerCase())) {
                        e.DrawElement(this.io);
                        found = true;
                    }
                }

                if (!found) {
                    this.io.SetTextColor(TextColor.MAGENTA);
                    this.io.WriteLine(" *No Elements Found");
                }

                this.io.SetTextColor(TextColor.WHITE);
                this.io.WriteLine("");
            }
        }

    }

    public void Hint() {
        Random rand = new Random();

        while(true) {
            Element E1 = (Element)this.UnlockedElements.get(rand.nextInt(this.UnlockedElements.size()));
            Element E2 = (Element)this.UnlockedElements.get(rand.nextInt(this.UnlockedElements.size()));
            Iterator var4 = AllElements.iterator();

            while(var4.hasNext()) {
                Element e = (Element)var4.next();
                if (e.TestElementCraftable(E1.GetName().toLowerCase(), E2.GetName().toLowerCase())) {
                    this.io.SetTextColor(TextColor.WHITE);
                    this.io.Write("\nTry combining ");
                    E1.DrawElement(this.io, false);
                    this.io.Write(" and ");
                    E2.DrawElement(this.io, false);
                    this.io.SetTextColor(TextColor.WHITE);
                    this.io.WriteLine("\n");
                    return;
                }
            }
        }
    }

    public static TextColor GetElementColor(String element) {
        Iterator var1 = AllElements.iterator();

        Element e;
        do {
            if (!var1.hasNext()) {
                return TextColor.WHITE;
            }

            e = (Element)var1.next();
        } while(!e.GetName().toLowerCase().equals(element.toLowerCase()));

        return e.GetColor();
    }
}
